package com.fa4j.third.base.service.impl;

import com.alibaba.fastjson2.TypeReference;
import com.fa4j.common.base.service.CommonSettingsService;
import com.fa4j.third.base.model.ThirdBizProps;
import com.fa4j.third.base.service.ThirdConfigService;
import lombok.RequiredArgsConstructor;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@RequiredArgsConstructor
public class ThirdCommonConfigServiceImpl implements ThirdConfigService {

    private final CommonSettingsService commonSettingsService;


    @Override
    public Optional<ThirdBizProps> getBizProps(String bizCode) {
        return commonSettingsService.getSettings(bizCode,
                        new TypeReference<HashMap<String, String>>() {
                        })
                .map(map -> {
                    ThirdBizProps props = new ThirdBizProps();
                    props.setBizCode(bizCode);
                    props.setAdapterName(map.get("adapterName"));
                    props.setAdapterParams(new HashMap<>());
                    for (Map.Entry<String, String> entry : map.entrySet()) {
                        if (!"adapterName".equals(entry.getKey()) && !"bizCode".equals(entry.getKey())) {
                            props.getAdapterParams().put(entry.getKey(), entry.getValue());
                        }
                    }
                    return props;
                });
    }

    @Override
    public boolean hasBizCode(String bizCode) {
        return commonSettingsService.getSettings(bizCode,
                        new TypeReference<HashMap<String, String>>() {
                        })
                .isPresent();
    }
}
